package com.media.publish.utils;

import android.graphics.Color;
import android.view.View;
import android.widget.ImageView;

import androidx.recyclerview.widget.ItemTouchHelper;
import androidx.recyclerview.widget.RecyclerView;

import com.baselibrary.utils.SpUtils;
import com.media.publish.adapter.ImageVideoAdapter;

import java.util.ArrayList;
import java.util.Collections;

/**
 * @author 关忆北丶
 * @description:
 * @date :2020/9/24 17:47
 */
public class RecyUtils {

    public static ItemTouchHelper itemTouchHelper(ImageVideoAdapter adapter, ArrayList<String> list, int maxCount, ImageView ivDrag) {
        return new ItemTouchHelper(new ItemTouchHelper.Callback() {
            @Override
            public int getMovementFlags(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder) {
                final int dragFlags = ItemTouchHelper.UP | ItemTouchHelper.DOWN |
                        ItemTouchHelper.LEFT | ItemTouchHelper.RIGHT;
                final int swipeFlags = 0;
                return makeMovementFlags(dragFlags, swipeFlags);
            }

            @Override
            public boolean onMove(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder, RecyclerView.ViewHolder target) {
                SpUtils.getInstance().putDynamicDrag();
                ivDrag.setVisibility(View.GONE);
                //得到当拖拽的viewHolder的Position
                int fromPosition = viewHolder.getAdapterPosition();
                //拿到当前拖拽到的item的viewHolder
                int toPosition = target.getAdapterPosition();
                if (maxCount != list.size() && toPosition == list.size()) {
                    return true;
                }
                if (fromPosition < toPosition) {//左边图片往右拖动
                    for (int i = fromPosition; i < toPosition; i++) {
                        if (maxCount != list.size()) {
                            if (i + 1 == list.size())
                                continue;
                        }
                        Collections.swap(list, i, i + 1);
                    }
                } else {//右边图片往左拖动
                    for (int i = fromPosition; i > toPosition; i--) {
                        if (maxCount != list.size()) {
                            if (i - 1 == -1)
                                continue;
                        }
                        Collections.swap(list, i, i - 1);
                    }
                }
                adapter.notifyItemMoved(fromPosition, toPosition);
                return true;
            }

            @Override
            public void onSwiped(RecyclerView.ViewHolder viewHolder, int direction) {

            }

            @Override
            public void onSelectedChanged(RecyclerView.ViewHolder viewHolder, int actionState) {
                super.onSelectedChanged(viewHolder, actionState);

                if (actionState != ItemTouchHelper.ACTION_STATE_IDLE)
                    viewHolder.itemView.setBackgroundColor(Color.LTGRAY);
            }

            @Override
            public void clearView(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder) {
                super.clearView(recyclerView, viewHolder);

                viewHolder.itemView.setBackgroundColor(0);
                adapter.setData(list);
                adapter.notifyDataSetChanged();
            }

            //重写拖拽不可用
            @Override
            public boolean isLongPressDragEnabled() {

                return false;
            }


        });
    }
}
